package org.school.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.school.dao.ShopDao;
import org.school.entity.Host;
import org.school.dao.HostDao;
import org.school.entity.Shop;
import org.school.entity.User;
import org.school.entity.message;
import org.school.service.HostService;
import org.school.staticException.PasswordIncorrectException;
import org.school.staticException.UserNotFoundException;
import org.school.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * (Host)表服务实现类
 *
 * @author makejava
 * @since 2022-12-29 00:54:25
 */
@Service("hostService")
public class HostServiceImpl implements HostService {
    @Resource
    private HostDao hostDao;
@Resource
private ShopDao shopDao;
    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Host queryById(Integer id) {
        return this.hostDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param host 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<Host> queryByPage(Host host, PageRequest pageRequest) {
        long total = this.hostDao.count(host);
        return new PageImpl<>(this.hostDao.queryAllByLimit(host, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param host 实例对象
     * @return 实例对象
     */
    @Override
    public Host insert(Host host) {
        this.hostDao.insert(host);
        return host;
    }

    /**
     * 修改数据
     *
     * @param host 实例对象
     * @return 实例对象
     */
    @Override
    public Host update(Host host) {
        this.hostDao.update(host);
        return this.queryById(host.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.hostDao.deleteById(id) > 0;
    }

    @Override
    public message register(Host regHost) throws NoSuchAlgorithmException {
        message mes=new message();
        Host reg=queryByName(regHost.getUsername());
        if(reg!=null){
            mes.setContent("用户已存在");
            mes.setStatus(0);
            return mes;
        }
        if(regHost.getUsername()!=null&&regHost.getPassword()!=null){
            regHost.setPassword(MD5Utils.getMD5Str(regHost.getPassword())); ;
            insert(regHost);}

        String token = UUID.randomUUID().toString();//生成唯一标识,暂时不用
        Host redisUser=regHost;
        redisUser.setToken(token);
        String userStr= JSONObject.toJSONString(redisUser);
        redisTemplate.opsForValue().set("usertoken:"+redisUser.getToken(), userStr,80*10, TimeUnit.MINUTES);
        mes.setType("商家");
        mes.setToken(token);
        mes.setStatus(1);
        mes.setContent("创建商家成功");
        return mes;

    }

    @Override
    public message login(String username, String password) throws UserNotFoundException, PasswordIncorrectException, NoSuchAlgorithmException {
       Host loginUser=queryByName(username);
       message newMsg=new message();
        newMsg.setType("商家");
        if(loginUser==null){
            newMsg.setContent("该用户不存在");
            newMsg.setStatus(-1);
            return newMsg;
        }
        //如果查到了账号但密码不对
        password=MD5Utils.getMD5Str(password);
        if(!password.equals(loginUser.getPassword())){
            newMsg.setContent("用户密码错误");
            newMsg.setStatus(-1);
            return newMsg;

        }
        String token = UUID.randomUUID().toString();//生成唯一标识,暂时不用
        Host redisUser=loginUser;
        redisUser.setToken(token);
        String userStr= JSONObject.toJSONString(redisUser);
        redisTemplate.opsForValue().set("usertoken:"+redisUser.getToken(), userStr,80*10, TimeUnit.MINUTES);

        newMsg.setStatus(1);
        newMsg.setContent(loginUser.getUsername());
        newMsg.setToken(token);
        return newMsg;
    }



    @Override
    public PageInfo<Shop> findShopInfo(String name, int pageNum, int size) {
        // TODO Auto-generated method stub
        PageHelper.startPage(pageNum, size);
        List<Shop> lists = this.shopDao.queryHostShopInfo(name);
        PageInfo<Shop> pageInfo = new PageInfo<>(lists);
        return pageInfo;
    }

    private Host queryByName(String username) {
        return this.hostDao.queryByName(username);
    }
}
